package com.hgd.evp.admin.biz;

import com.hgd.evp.admin.entity.EDepart;
import com.hgd.evp.admin.mapper.EDepartMapper;
import com.hgd.evp.admin.vo.DeptTree;
import com.hgd.evp.common.biz.BusinessBiz;
import com.hgd.evp.common.utils.EmptyUtil;
import com.hgd.evp.common.utils.ObjectResultResponse;
import com.hgd.evp.common.utils.TreeUtil;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author By Shang
 * @email ${email}
 * @version 2019-01-01 22:49:10
 */
@Service
public class EDepartBiz extends BusinessBiz<EDepartMapper,EDepart> {
    /**
     * 添加部门
     * @param eDepart 待添加部门
     * @return 结果
     */
    public ObjectResultResponse<Void> addEDepart(EDepart eDepart) {
        ObjectResultResponse<Void> oResult=new ObjectResultResponse<>();
        this.insertSelectiveBindCrt(eDepart);
        oResult.setMessage("添加部门成功");
        return oResult;
    }

    /**
     * 获取部门树
     * @return 部门树
     */
    public List<DeptTree> getDepartTree() {
        List<EDepart> allDepart = getAllDepart();

        List<DeptTree> departTrees=new ArrayList<>();
        allDepart.forEach(depart-> departTrees.add(
                new DeptTree(depart.getId(), depart.getParentId(), depart.getEName())
        ));

        return TreeUtil.buildTree(departTrees, -1);
    }

    /**
     * 获取全部在使用部门
     * @return 全部在使用部门
     */
    public List<EDepart> getAllDepart(){
        Example example=new Example(EDepart.class);

        List<EDepart> eDeparts = this.selectByExample(example);
        if(EmptyUtil.isEmpty(eDeparts)){
            return new ArrayList<>();
        }

        return eDeparts;
    }
}